package com.smartkey.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.project.smartkey.R;
import com.smartkey.adapter.AdapterAppPick;
import com.smartkey.bean.AppInfo;
import com.smartkey.utils.AppUtil;
import com.smartkey.utils.ClickType;

public class AppPickUserCustomerActivity extends BaseActivity {

	private ListView listview;
	private ArrayList<AppInfo> appList;
	private AdapterAppPick adapter;

	private ClickType clickType;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_list_layout);
		initParam();
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		initData();
		showApplist();
	}

	private void initParam() {
		Intent intent = getIntent();
		if (intent == null) {
			return;
		}
		String clickTypeStr = intent.getStringExtra("clickType");
		try {
			clickType = ClickType.valueOf(clickTypeStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initView() {
		listview = (ListView) findViewById(R.id.app_list_listview);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				AppInfo appInfo = (AppInfo) adapter.getItem(arg2);
				String checkAppPackageName = appInfo.packageName;
				System.out
						.println("########### AppListActivity#####  checkAppPackageName = "
								+ checkAppPackageName);
				AppUtil.setCheckedApp(AppPickUserCustomerActivity.this,
						appInfo, clickType);
				// finish();
				showApplist();
			}
		});
		findViewById(R.id.img_back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initData() {
		appList = (ArrayList<AppInfo>) AppUtil.getNonSystemAppInfo(this);
	}

	private void showApplist() {
		if (appList != null) {
			adapter = new AdapterAppPick(this, clickType, false);
			listview.setAdapter(adapter);
			adapter.notifyDataSetChanged();
		}
	}
}
